home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / c / cujoct93.zip / 1110077B < prev    next >
Text File  |  1993-01-19  |  4KB  |  156 lines

  1. /********** DISPLAYS.C **********/
  2. #include <graphics.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include <dos.h>
  6. #include "randefs.h"
  7. #include "displays.h"
  8.  
  9. static int  h, w;    /* char height and width */
  10.  
  11. void NewScreen(char *title)
  12. { h = textheight("0")-1, w = textwidth("0");
  13.   cleardevice();
  14.   setcolor(BLUE);
  15.   setbkcolor(WHITE);
  16.   rectangle(0, 0, SPAN, FS);  /* Draw boundary   */
  17.   outtextxy(5*w, -7*h,"Hit any key to continue, ESC to exit");
  18.   /***** Display Title: *****/
  19.   outtextxy(5*w, -2*h, title);
  20.   randomize();
  21. }
  22.  
  23. /*****************************************************
  24.  * RandWalk calls SumNPicks to generate random events
  25.  * per the model in table pointed to by rnd.
  26.  *****************************************************/
  27. int RandWalk(RAND *rnd)
  28. { int    oldx, oldy, x, y, sf, nsteps, nbin = rnd->nbin;
  29.   double theta;     /* angle in radians */
  30.  
  31.   /************* Display Random Walk *************/
  32.   NewScreen(*(rnd->labls+nbin));
  33.   line(SPAN/2, 0, SPAN/2, FS);/* Vert Centr Line */
  34.   line(0, FS/2,SPAN, FS/2);   /* Horiz C.L.      */
  35.   oldx = x = SPAN/2;          /* Start at Origin */
  36.   oldy = y = FS/2;            /* Ditto           */
  37.   nsteps = 0;
  38.  
  39.   sf = (nbin < 12) ? 22/nbin : 1;  /* Scaling Factor */
  40.  
  41.   while((x<SPAN)&&(x>0)&&(y<FS)&&(y>0)&& (kbhit()==0) )
  42.   {  if(strcmp(*(rnd->labls+nbin),"UNIFORM CIRCULAR:"))
  43.      {  x = oldx + sf*(2*SumNPicks(rnd) - (nbin-1));
  44.         y = oldy + sf*(2*SumNPicks(rnd) - (nbin-1));
  45.      }
  46.        else
  47.        {  theta = K0*((double)( 1+SumNPicks(rnd) ));
  48.            /***** Convert to Cartesian Coord's *****/
  49.            x = oldx + 15 * cos(theta); /* 15 = step len */
  50.            y = oldy + 15 * sin(theta);
  51.      }
  52.      line(oldx, oldy, x, y);
  53.      nsteps++;
  54.      oldx = x;
  55.      oldy = y;
  56.      delay(50);  /* User needs time to discern step */
  57.   }
  58.   ResetTxtCursor();
  59.   printf("\n\n NSTEPS = %d\n", nsteps);
  60.   if (ESC == getch())  /* User's chance to exit */
  61.      return(1);
  62.   return(0);
  63. }
  64.  
  65. void Erase(int x, int y, int scanlen, int nlins)
  66. { int  i, j;
  67.  
  68.   moveto(x, y);
  69.   for (i = 0; i < scanlen; i++)
  70.      for (j = 0; j < nlins; j++)
  71.         putpixel(x + i, y + j, BLACK);
  72. }
  73.  
  74. void ScreenLabels(void)
  75. { outtextxy(-135, 0.09*FS, " DIFFERENTIATOR");
  76.   outtextxy(-135, 0.31*FS, "LOW-PASS FILTER");
  77.   outtextxy(-135, 0.53*FS, "RUNNING AVERAGE");
  78.   outtextxy(-135, 0.75*FS, "   NOISY SIGNAL");
  79.   outtextxy(-135, 0.97*FS, "         SIGNAL");
  80. }
  81.  
  82. void ResetTxtCursor(void)
  83. { union REGS r;
  84.  
  85.   r.h.ah = 2;   /* Function 2 */
  86.   r.h.bh = 0;   /* Page   = 0 */
  87.   r.h.dh = 0;   /* Row    = 0 */
  88.   r.h.dl = 0;   /* Column = 0 */
  89.   int86(0x10, &r, &r);
  90. }
  91.  
  92. void DrawAxes(RAND *rnd)
  93. { int  i, h_off, h_step, nbin = rnd->nbin;
  94.  
  95.   h_step = SPAN/nbin;
  96.   h_off = (nbin < 25) ? h_step/4 : 0;
  97.   line(-1, FS, SPAN, FS);
  98.   line(-1, FS, -1,   0);
  99.   outtextxy(-5*w,        - h, "1.0 _");
  100.   outtextxy(-5*w, 0.2*FS - h, "0.8 _");
  101.   outtextxy(-5*w, 0.4*FS - h, "0.6 _");
  102.   outtextxy(-5*w, 0.6*FS - h, "0.4 _");
  103.   outtextxy(-5*w, 0.8*FS - h, "0.2 _");
  104.   outtextxy(-5*w,     FS - h, "0.0 _");
  105.   outtextxy(30*w, -2*h, "N_TRIALS =");
  106.   for (i = 0; i < nbin; i++)
  107.        outtextxy(h_off+i*h_step,FS+6,*(rnd->labls+i));
  108. }
  109.  
  110. void DrawData(int *data, int y_offset)
  111. { int  *dat = data;
  112.   int  ndat = *dat++;
  113.   int  x, y, oldx = 0, oldy = y_offset;
  114.  
  115.   for (x = 0; x < ndat; x++)
  116.   {  y = y_offset + *dat++;
  117.      line(oldx, oldy, x, y);
  118.      oldx = x;
  119.      oldy = y;
  120.   }
  121. }
  122.  
  123. void DisplayFreq(RAND *rnd, float *frqdst)
  124. { int  i, y, nbin = rnd->nbin;
  125.   int  delx = SPAN/nbin;
  126.   char buf[6];
  127.  
  128.   clearviewport();
  129.   if ( !(rnd->ndat%10) )
  130.   {  Erase(41*w, -2*h, 40, 8);
  131.        itoa(rnd->ndat, buf, 10);
  132.        outtextxy(41*w, -2*h, buf);
  133.   }
  134.  
  135.   for (i = 0; i < nbin; i++)
  136.   {  y = (1 - *(frqdst+i))*FS;
  137.        if(y > 0 && y != FS)    /* Stay inside borders */
  138.         line(i*delx, y, (i+1)*delx, y);
  139.   }
  140. }
  141.  
  142. void DisplayStats(RAND *rnd, float *data)
  143. /************** Print Statistics **************/
  144. { int    i;
  145.   float  sum = 0.0;
  146.  
  147.   ResetTxtCursor();
  148.   printf("Event   Frequency");
  149.   for (i = 0; i < rnd->nbin; i++)
  150.   {  printf("\n%s\t   %.3f\%", *(rnd->labls+i), *(data+i));
  151.      sum += *(data+i)*rnd->delta;
  152.   }
  153.   printf("\nINTEGRAL = %.3f\%", sum);
  154. }
  155.  
  156.